local love = require 'love'

local cash
local font
local salary
local salaryNum
local rechargeNum
local padding
local buttonWidth
local buttonHeight
local infoWidth
local infoHeight
 -- 设置窗口比例参考iphone14
local aspectRatio = 2.17
local baseHeight = 600
local baseWidth = baseHeight / aspectRatio

-- 窗口配置
function love.load()
    love.window.setTitle("Cash Game")

    love.window.setMode(baseWidth, baseHeight, {resizable = false, borderless = false, centered = true})

    -- 加载支持中文的字体
    local fontPath = "assets/simhei.ttf" -- 确保字体文件在项目目录下
    font = love.graphics.newFont(fontPath, 24)
    love.graphics.setFont(font)

    -- 初始化现金余额
    cash = 0
    -- 初始化按键历史
    salaryNum = 0
    rechargeNum = 0
    -- 按钮参数
    padding = 10
    buttonWidth = baseWidth / 2 - padding * 1.5
    buttonHeight = buttonWidth / aspectRatio
    baseWidth = love.graphics.getWidth()
    baseHeight = love.graphics.getHeight()
    -- 信息框参数
    infoWidth = baseWidth - padding * 2
    infoHeight = buttonHeight / 2 + padding
end

-- 绘制函数
function love.draw()
    -- 绘制渐变背景
    local numRectangles = 10
    for i = 1, numRectangles do
        local y = (i - 1) * (baseHeight / numRectangles)
        local height = baseHeight / numRectangles
        -- 从蓝色渐变到白色
        local r = 0.2 + (i - 1) * (0.8 / numRectangles)
        local g = 0.4 + (i - 1) * (0.6 / numRectangles)
        local b = 0.6 + (i - 1) * (0.4 / numRectangles)
        love.graphics.setColor(r, g, b)
        love.graphics.rectangle("fill", 0, y, baseWidth, height)
    end

    -- 绘制现金余额
    love.graphics.setColor(1, 1, 1)
    love.graphics.rectangle("fill", padding, padding, infoWidth, infoHeight)
    love.graphics.setColor(0, 0, 0)
    love.graphics.printf("卡里余额: " .. cash, padding, padding * 2, baseWidth - padding * 4, "left")

    -- 绘制按键历史
    love.graphics.setColor(90/255,98/255,106/255)
    love.graphics.rectangle("fill", padding, baseHeight - buttonHeight - padding - infoHeight - padding, buttonWidth, infoHeight)
    love.graphics.setColor(0, 0, 0)
    love.graphics.printf("透支了" .. salaryNum .. "月", padding, baseHeight - buttonHeight - padding - infoHeight, buttonWidth + (infoHeight - padding) / 2, "center")
    love.graphics.setColor(90/255,98/255,106/255)
    love.graphics.rectangle("fill", baseWidth - buttonWidth - padding, baseHeight - buttonHeight - padding - infoHeight - padding, buttonWidth, infoHeight)
    love.graphics.setColor(0, 0, 0)
    love.graphics.printf("已" .. rechargeNum .. "次充值", baseWidth - buttonWidth - padding * 2, baseHeight - buttonHeight - padding - infoHeight, buttonWidth + (infoHeight - padding) / 2, "center")

    -- 绘制 "Get Salary" 按钮
    love.graphics.setColor(141/255,194/255,31/255)
    love.graphics.rectangle("fill", padding, baseHeight - buttonHeight - padding, buttonWidth, buttonHeight)
    love.graphics.setColor(0, 0, 0)
    love.graphics.printf("透支工资", padding, baseHeight - buttonHeight - padding + (buttonHeight - padding) / 2, buttonWidth, "center")

    -- 绘制 "Recharge 648" 按钮
    love.graphics.setColor(0,132/255,207/255)
    love.graphics.rectangle("fill", baseWidth - buttonWidth - padding, baseHeight - buttonHeight - padding, buttonWidth, buttonHeight)
    love.graphics.setColor(0, 0, 0)
    love.graphics.printf("充值648", baseWidth - buttonWidth - padding, baseHeight - buttonHeight - padding + (buttonHeight - padding) / 2, buttonWidth, "center")
end

-- 鼠标点击事件
function love.mousepressed(x, y, button)
    salary = 2200
    if button == 1 then -- 左键点击
        -- 检查 "Get Salary" 按钮点击
        if x >= padding and x <= padding + buttonWidth and y >= baseHeight - buttonHeight - padding and y <= baseHeight - padding then
            cash = cash + salary
            salaryNum = salaryNum + 1
        end

        -- 检查 "Recharge 648" 按钮点击
        if x >= baseWidth /2 + padding * 0.5 and x <= baseWidth - padding and y >= baseHeight - buttonHeight - padding and y <= baseHeight - padding then
            if cash >= 648 then
                cash = cash - 648
                rechargeNum = rechargeNum + 1
            end
        end
    end
end